package io.jsen.meta.diff.support;

import static io.jsen.meta.diff.support.MetaModifier.STATE_0;
import static io.jsen.meta.diff.support.MetaModifier.STATE_1;

/**
 * @author jsen
 * @date 2021/05/01
 **/
public class MetaChangeModifier {

    public static boolean isNoChange(int mod) {
        return (STATE_0 & mod) == 0 && (STATE_1 & mod) == 0;
    }

    public static int setNoChange(int mod) {
        mod = MetaModifier.setMod(mod, STATE_0, false);
        mod = MetaModifier.setMod(mod, STATE_1, false);
        return mod;
    }

    public static boolean isChange(int mod) {
        return (STATE_0 & mod) != 0 && (STATE_1 & mod) == 0;
    }

    public static int setChange(int mod) {
        mod = MetaModifier.setMod(mod, STATE_0, true);
        mod = MetaModifier.setMod(mod, STATE_1, false);
        return mod;
    }

    public static boolean isInsert(int mod) {
        return (STATE_0 & mod) == 0 && (STATE_1 & mod) != 0;
    }

    public static int setInsert(int mod) {
        mod = MetaModifier.setMod(mod, STATE_0, false);
        mod = MetaModifier.setMod(mod, STATE_1, true);
        return mod;
    }

    public static boolean isDelete(int mod) {
        return (STATE_0 & mod) != 0 && (STATE_1 & mod) != 0;
    }

    public static int setDelete(int mod) {
        mod = MetaModifier.setMod(mod, STATE_0, true);
        mod = MetaModifier.setMod(mod, STATE_1, true);
        return mod;
    }

    public static int resetState(int mod, int stateModifier) {
        int ne0 = mod ^ STATE_0;
        int ne1 = mod ^ STATE_1;
        mod = mod & ne0 & ne1;
        return mod | stateModifier;
    }

    public static String getDesc(int mod) {
        if (isDelete(mod)) {
            return "delete";
        } else if (isChange(mod)) {
            return "change";
        } else if (isInsert(mod)) {
            return "insert";
        } else {
            return "no change";
        }
    }

    private MetaChangeModifier() {
    }
}
